package com.qsmx.store.app.main;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;

import com.qsmx.store.app.R;
import com.qsmx.store.app.R2;
import com.qsmx.store.app.event.HomeTitleShowEvent;
import com.qsmx.store.app.event.HomeUnLockEvent;
import com.qsmx.store.app.event.SetShowMaxEvent;
import com.qsmx.store.app.fustion.FustionManager;
import com.qsmx.store.app.main.gift.GiftDelegate;
import com.qsmx.store.app.main.goods.GoodsDelegate;
import com.qsmx.store.app.main.home.HomeDelegate;
import com.qsmx.store.app.main.home.HomeLockedDelegate;
import com.qsmx.store.app.main.mem.MemCardDelegate;
import com.qsmx.store.app.main.service.ServiceCoinDelegate;
import com.qsmx.store.app.main.service.ServiceHomeDelegate;
import com.qsmx.store.app.main.suit.SuitDelegate;
import com.qsmx.store.core.delegates.AtmosDelegate;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class AppHomeDelegate extends AtmosDelegate {


    private PopupWindow mPopupWindow;
    private AppCompatTextView tvLock;

    @BindView(R2.id.lin_title_content)
    LinearLayoutCompat linTitleContent;
    @BindView(R2.id.cl_head_info)
    ConstraintLayout clHeadInfo;
    @BindView(R2.id.cl_title)
    ConstraintLayout clTitle;

    @BindView(R2.id.tv_coin_sale)
    AppCompatTextView tvCoinSale;
    @BindView(R2.id.tv_mem_card)
    AppCompatTextView tvMemCard;
    @BindView(R2.id.tv_goods_sale)
    AppCompatTextView tvGoodsSale;
    @BindView(R2.id.tv_gift)
    AppCompatTextView tvGift;
    @BindView(R2.id.tv_ticket_sale)
    AppCompatTextView tvTicketSale;
    @BindView(R2.id.tv_system_set)
    AppCompatTextView tvSystemSet;

    @OnClick(R2.id.tv_coin_sale)
    void onCoinSale() {
        onSetTitleCheckBg(tvCoinSale);
        getSupportDelegate().replaceChildFragment(new SuitDelegate(), false);
    }

    @OnClick(R2.id.tv_mem_card)
    void onMemCard() {
        onSetTitleCheckBg(tvMemCard);
        getSupportDelegate().replaceChildFragment(new MemCardDelegate(), false);
    }

    @OnClick(R2.id.tv_goods_sale)
    void onGoodsSale() {
        onSetTitleCheckBg(tvGoodsSale);
        getSupportDelegate().replaceChildFragment(new GoodsDelegate(), false);
    }

    @OnClick(R2.id.tv_gift)
    void onGift() {
        onSetTitleCheckBg(tvGift);
        getSupportDelegate().replaceChildFragment(new GiftDelegate(), false);
    }

    @OnClick(R2.id.tv_ticket_sale)
    void onTicketSale() {
        onSetTitleCheckBg(tvTicketSale);
    }

    @OnClick(R2.id.tv_system_set)
    void onSystemSet() {
        onSetTitleCheckBg(tvSystemSet);
        getSupportDelegate().replaceChildFragment(new ServiceHomeDelegate(), false);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_app_home;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        initPopupWindow();
        linTitleContent.setVisibility(View.GONE);
        getSupportDelegate().loadRootFragment(R.id.delegate_container, new HomeDelegate());

    }

    @SuppressLint("SetTextI18n")
    private void initPopupWindow() {

        mPopupWindow = new PopupWindow(getContext());
        View dialogLoginInfoView = View.inflate(getContext(), R.layout.dialog_login_info, null);
        mPopupWindow.setContentView(dialogLoginInfoView);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_corner_dialog_bg));//设置背景
        clHeadInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.showAsDropDown(clTitle, 1600, 0);
            }
        });

        tvLock = dialogLoginInfoView.findViewById(R.id.tv_lock);
        tvLock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
                linTitleContent.setVisibility(View.GONE);
                tvLock.setVisibility(View.GONE);
                getSupportDelegate().startChild(new HomeLockedDelegate());
            }
        });

        AppCompatTextView tvLoginOut = dialogLoginInfoView.findViewById(R.id.tv_login_out);
        tvLoginOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
                getSupportDelegate().pop();
            }
        });

        AppCompatTextView tvPr = dialogLoginInfoView.findViewById(R.id.tv_pr);
        AppCompatTextView tvAccount = dialogLoginInfoView.findViewById(R.id.tv_account);
        tvPr.setText("权限：" + FustionManager.mLoginData.getUser().getUser().getRoles().get(0).getName());
        tvAccount.setText("账号：" + FustionManager.mLoginData.getUser().getUser().getUsername());
    }


    public void onSetTitleCheckBg(AppCompatTextView tv) {
        tvCoinSale.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_corner_title_white_bg));
        tvMemCard.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_corner_title_white_bg));
        tvGoodsSale.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_corner_title_white_bg));
        tvGift.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_corner_title_white_bg));
        tvTicketSale.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_corner_title_white_bg));
        tvSystemSet.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_corner_title_white_bg));

        tvCoinSale.setTextColor(getResources().getColor(R.color.theme_black));
        tvMemCard.setTextColor(getResources().getColor(R.color.theme_black));
        tvGoodsSale.setTextColor(getResources().getColor(R.color.theme_black));
        tvGift.setTextColor(getResources().getColor(R.color.theme_black));
        tvTicketSale.setTextColor(getResources().getColor(R.color.theme_black));
        tvSystemSet.setTextColor(getResources().getColor(R.color.theme_black));

        tv.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_corner_title_yellow_bg));
        tv.setTextColor(getResources().getColor(R.color.theme_white));
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(HomeTitleShowEvent event) {
        if (event != null && event.getData() != null) {
            linTitleContent.setVisibility(View.VISIBLE);
            String title = event.getData().getString("title");
            if (title.equals("mem_card")) {
                onSetTitleCheckBg(tvMemCard);
            } else if (title.equals("suit")) {
                onSetTitleCheckBg(tvCoinSale);
            } else if (title.equals("goods")) {
                onSetTitleCheckBg(tvGoodsSale);
            } else if (title.equals("service")) {
                onSetTitleCheckBg(tvSystemSet);
            } else if (title.equals("gift")) {
                onSetTitleCheckBg(tvGift);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(HomeUnLockEvent event) {
        if (event != null && event.getData() != null) {
            if (mPopupWindow != null && tvLock != null) {
                tvLock.setVisibility(View.VISIBLE);
                linTitleContent.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
